package list3;

public class MysingleList implements IList{

    // public ListNode last;  //链表的尾节点
    public ListNode head;   //链表的头节点-->链表的属性
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }

    @Override
    public void addLast(int data) {
        //使用尾插法时要注意判断链表是否为空
        ListNode node = new ListNode(data);

        if(head==null){
            head = node;
        }else {
            ListNode cur = head;
            while (cur.next!=null){//while(cur!=null)
                cur = cur.next;
            }
            cur.next = node;//cur=node;
        }


    }

    @Override
    public void addIndex(int index, int data) throws IndexException{
        //cur节点如果已经指向后一个节点,那么是无法指向前一个节点的

        //1.错误解法
        /*ListNode node = new ListNode(data);
        int flag = 0;
        ListNode cur = head;
        while(cur!=null){
            flag++;
            cur=cur.next;
        }
        if(index>flag){
            System.out.println("超出数组范围!");
        }else{
            for(int i=0;i<flag;i++){
                if(i==index){
                    node.next = cur.next;
                    cur.next = node;
                }
            }
        }*/

        //2.正确解法
        if(index<0||index>size()){
            throw new IndexException("index不合法"+index);
        }
        ListNode node = new ListNode(data);
        if(head==null){
            head=node;
            return;
        }
        if(index==0){
            addFirst(data);
            return;
        }
        if(index==size()){//index是要插入的下标位置  size是整个链表的长度
            addLast(data);
            return;
        }
        //中间插入
        ListNode cur = searchPrevIndex(index);//返回index前一个目标节点
        node.next=cur.next;
        cur.next=node;
    }

    private ListNode searchPrevIndex(int index){
        ListNode cur=head;
        int count=0;
        while(count!=index-1){
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur!=null){
            if(cur.value==key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        //只移掉第一个
        if(head==null){
            return;
        }
        if(head.value==key){
            head=head.next;
            return;
        }
        ListNode cur = findPrevKey(key);
        cur.next = cur.next.next;
    }

    private ListNode findPrevKey(int key) {
        ListNode cur = head;
        while (cur.next!=null){
            if(cur.next.value==key){
                break;
            }
            cur=cur.next;
        }
        return cur;
    }

    @Override
    public void removeAIIKey(int key) {
        //删除所有关键字的节点
        ListNode prev = head;
        ListNode cur = head.next;
        while (cur!=null){
            //因为要对比所有节点的value,所以要遍历所有节点(cur!=null),如果使用(cur.next!=null),则最后一个节点无法进行比较
            if(cur.value==key){
                prev.next = cur.next;
                cur=cur.next;
            }else{
                prev = cur;
                cur = cur.next;
            }
        }
    }

    @Override
    public int size() {
        int i=0;
        ListNode cur = head;
        while(cur!=null){
            i++;
            cur = cur.next;
        }
        return i;
    }

    @Override
    public void clear() {
        /*while(head!=null) {
            System.out.println(head.value + " ");
            head = head.next;
        }*/
        head=null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur!=null){
            System.out.print(cur.value+" ");
            cur = cur.next;
        }
    }

    public ListNode reverList(){
        //用来翻转链表
        if(head==null){
            return head;
        }
        if (head.next==null){
            return head;
        }
        ListNode cur = head.next;
        head.next=null;

        while(cur!=null){
            ListNode curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    public ListNode middleNode(){
        //寻找中间节点,如果有两个中间节点,那么返回第二个中间节点
        ListNode fast = head;
        ListNode slow =head;

        /*//获取链表长度
        int i=0;
        ListNode cur = head;
        while(cur!=null){
            i++;
            cur = cur.next;
        }
        if(i%2==0){//链表长度为双数
            while(fast!=null){
                fast = fast.next.next;
                slow = slow.next;
            }
        }else{
            while (fast.next!=null){
                fast = fast.next.next;
                slow = slow.next;
            }
        }*/

        while(fast!=null&&fast.next!=null){//二者不能互换,条件不一样
            fast = fast.next.next;
            slow = slow.next;
        }

        return slow;
    }

    public ListNode KeyNode(int key) throws KthTotailException{
        //输出链表中倒数的第K个结点
        if(key<=0){
            throw new KthTotailException("关键字不合法");
        }
        if(head==null){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        int count = 0;
        while(count!=key-1){
            if(fast.next!=null){
                fast=fast.next;
                count++;
            }else{//用来判断key过大
                throw new KthTotailException("关键字不合法");
            }
        }
        while(fast.next!=null){
            fast = fast.next;
            slow  =slow.next;
        }
        return slow;
    }

    public ListNode GetTogether(ListNode HeadA,ListNode HeadB){
        //给到两个递增排列链表,要求合并两个链表,新链表仍然递增,最后返回新链表的头指针
        ListNode newNode = new ListNode(-1);//使用一个备用头节点
        ListNode node = newNode;
        if (HeadA==null){
            return HeadB;
        }
        if (HeadB==null){
            return HeadA;
        }

        while (HeadA!=null&&HeadB!=null){
            if(HeadA.value< HeadB.value){
                node.next=HeadA;
                HeadA = HeadA.next;
                node = node.next;
            }else{
                node.next=HeadB;
                HeadB= HeadB.next;
                node = node.next;
            }
        }
        if(HeadA!=null){
            node.next = HeadA;
        }
        if (HeadB!=null){
            node.next=HeadB;
        }

        return newNode.next;
    }

   /* public ListNode partition(int x){
        //已知一个链表,输入x,将链表中比x小的所有节点全部排在其余节点之前,且不改变原来的数据顺序,返回重新排列后的链表的头指针
        //定义一个cur 指向链表的头节点
        if(head==null){
            return null;
        }
        ListNode cur = head;

        ListNode bs =null;//before start
        ListNode be =null;//before end
        ListNode as =null;//after start
        ListNode ae =null;//after end
        //使用cur来遍历所有节点
        //尾插法
        while(cur!=null){
            if(cur.value<x){
                if(bs==null){
                    bs=cur;
                    be=cur;
                }else{
                    be.next = cur;
                    be = be.next;
                }
            }else{
                //>=x
                if(as==null){
                    as=ae=cur;
                }else{
                    ae.next = cur;
                    ae=ae.next;
                }
            }
            cur = cur.next;
        }
        if(bs==null){
            return as;
        }
        if (as!=null){
            ae.next=null;
        }
        be.next = as;
        return bs;
    }*/

    public ListNode partition(int x) {
        //将所有小于x的结点排在其余结点之前
        if(head==null){
            return null;
        }

        ListNode bs = null;//before start
        ListNode be = null;//before end;
        ListNode as = null;//after start
        ListNode ae = null;//after end

        ListNode cur = head;

        while(cur!=null){
            if(cur.value<x){
                if(bs==null){
                    bs = cur;
                    be = cur;
                }else{
                    be.next = cur;
                    be = be.next;
                }
            }else {
                if(as==null){
                    as = cur;
                    ae = cur;
                }else{
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        if(bs==null){
            return as;
        }
        be.next = as;
        if(as!=null){
            ae.next = null;
        }
        return bs;
    }

    public boolean chkPalindrome() {
        //判断链表是否为回文
        if(head==null){
            return false;
        }

        ListNode fast = head;
        ListNode slow = head;

        while (fast!=null&&fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode cur = slow.next;

        while (cur!=null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur=curNext;
        }

        while (slow!=head){
            if(head.value!=slow.value){
                return false;
            }
            if(head.next==slow){
                return true;
            }
            head = head.next;
            slow = slow.next;
        }

        return true;
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //找出并返回两个单链表相交的起始节点
        int flag1 = 0;
        int flag2 = 0;
        int count = 0;

        if(headA==null){
            return null;
        }
        if(headB==null){
            return null;
        }
        if(headA==headB){
            return headA;
        }

        ListNode cur1  =headA;
        ListNode cur2 = headB;
        while (cur1!=null){
            flag1++;
            cur1 = cur1.next;
        }
        while (cur2!=null){
            flag2++;
            cur2 = cur2.next;
        }
        cur1=headA;
        cur2=headB;
        if(flag1<flag2){
            count = flag2-flag1;
            int i=0;
            while(cur1!=null&&cur2!=null){
                while (i<count){
                    i++;
                    cur2=cur2.next;
                }
                if(cur1==cur2){
                    break;
                }
                cur1=cur1.next;
                cur2=cur2.next;

            }
        }else {
            count = flag1-flag2;
            int i=0;
            while(cur1!=null&&cur2!=null){
                while (i<count){
                    i++;
                    cur1=cur1.next;
                }
                if(cur1==cur2){
                    break;
                }
                cur1=cur1.next;
                cur2=cur2.next;

            }
        }
        return cur1;
    }


    public boolean hasCycle() {
        //判断链表中是否有环
        if(head==null){
            return false;
        }
        if(head.next==null){
            return false;
        }
        ListNode fast = head;
        ListNode slow=head;

        //判断相遇
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){
                return true;
            }
        }
        return false;
    }

    public ListNode detectCycle() {
        if(head==null){
            return null;
        }
        if(head.next==null){
            return null;
        }
        ListNode fast = head;
        ListNode slow=head;

        //判断相遇
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow){
                break;
            }
        }
        if(fast==null||fast.next==null){
            return null;
        }
        slow=head;
        while (slow!=fast){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }

    //创建一个链表
    static class ListNode{
        public int value;
        public ListNode next;//让next的类型为ListNode
        public ListNode prev;
        public ListNode(int value){//构造方法
            this.value =value;
        }
    }//每一个节点为一个内部类


}
